package com.dpj.edu.controller;


import com.dpj.commonutils.JwtUtils;
import com.dpj.commonutils.Result;
import com.dpj.commonutils.ordervo.UcenterMemberOrder;
import com.dpj.edu.client.UcenterClient;
import com.dpj.edu.entity.Comment;
import com.dpj.edu.service.CommentService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-04-11
 */

@RestController
@RequestMapping("/edu/comment")
public class CommentController {
        @Autowired
        private CommentService commentService;
        @Autowired
        private UcenterClient ucenterClient;

    //根据课程id进行分页查询课程评论
    @GetMapping("/{page}/{limit}")
    public Result getComments(@PathVariable Long page,@PathVariable Long limit, String courseId){
        Map map=commentService.getComments(page,limit,courseId);
        return Result.ok().data(map);
    }
    @ApiOperation(value = "添加评论")
    @PostMapping("auth/save")
    public Result addComment(@RequestBody Comment comment, HttpServletRequest request){
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if(StringUtils.isEmpty((memberId))){
            return Result.error().message("请登录");
        }
        comment.setMemberId(memberId);
        UcenterMemberOrder member = ucenterClient.getUserInfoOrder(memberId);
        comment.setNickname(member.getNickname());
        comment.setAvatar(member.getAvatar());
        commentService.save(comment);
        return Result.ok();

    }
}

